//
//  ViewController.m
//  HUD
//
//  Created by niit on 16/3/28.
//  Copyright © 2016年 NIIT. All rights reserved.
//

#import "ViewController.h"
#import <MBProgressHUD.h>
#import <SVProgressHUD.h>

@interface ViewController ()<MBProgressHUDDelegate>
{
    MBProgressHUD *hud;
}
@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}
- (IBAction)bt1Pressed:(id)sender {
    // 创建进度条
    hud = [[MBProgressHUD alloc] initWithView:self.view];
    // 加入到当前视图
    [self.view addSubview:hud];
    // 背景变暗
    hud.dimBackground = YES;
    // 文字
    hud.labelText = @"请稍后";
    // 显示出来
    [hud showAnimated:YES whileExecutingBlock:^{
        // 显示后执行的费时操作
        sleep(3);// => [NSThread sleepForTimeInterval:3];
        
    } completionBlock:^{
        // 操作完了之后
        [hud removeFromSuperview];
        hud = nil;
    }];

}
- (IBAction)bt2Pressed:(id)sender {
    hud = [[MBProgressHUD alloc] initWithView:self.view];
    [self.view addSubview:hud];
    hud.dimBackground = YES;
    hud.mode = MBProgressHUDModeDeterminate;
    hud.labelText = @"丁金铭正在变形";
    hud.delegate = self;
    [hud showWhileExecuting:@selector(myTask) onTarget:self withObject:nil animated:YES];
}
- (void)myTask
{
    float progress = 0.0f;
    while (progress < 1.0f) {
        progress += 0.01f;
        hud.progress = progress;
        usleep(50000);
    }
}

- (void)hudWasHidden:(MBProgressHUD *)hud
{
    [hud removeFromSuperview];
    hud = nil;
}


- (IBAction)bt3Pressed:(id)sender {
    hud = [[MBProgressHUD alloc] initWithView:self.view];
    [self.view addSubview:hud];
    hud.dimBackground = YES;
    hud.mode = MBProgressHUDModeCustomView;
    hud.labelText = @"丁金铭某新噶";
    hud.customView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"checkmakr3"]];
    [hud showAnimated:YES whileExecutingBlock:^{
        sleep(2);
    } completionBlock:^{
        [hud removeFromSuperview];
        hud = nil;
    }];
}
- (IBAction)bt4Pressed:(id)sender {
    hud = [[MBProgressHUD alloc] initWithView:self.view];
    [self.view addSubview:hud];
    
    hud.dimBackground = YES;
    hud.mode = MBProgressHUDModeDeterminate;
    hud.labelText = @"djm没有小鸡鸡";
    hud.yOffset = 50.0f;
    hud.xOffset = 50.0f;
    [hud showAnimated:YES whileExecutingBlock:^{
        sleep(2);
    } completionBlock:^{
        [hud removeFromSuperview];
        hud = nil;
    }];
}

- (IBAction)bt5Pressed:(id)sender {
//    [SVProgressHUD showInfoWithStatus:@"abc123"];
//     [SVProgressHUD showSuccessWithStatus:@"abc123"];
//    [SVProgressHUD dismiss];
    [SVProgressHUD showErrorWithStatus:@"abc123"];
}
- (IBAction)bt6Pressed:(id)sender {
    [SVProgressHUD showWithStatus:@"正在登陆"];
    [SVProgressHUD setDefaultAnimationType:SVProgressHUDMaskTypeBlack]; // 背景变暗
    dispatch_queue_t queue = dispatch_get_global_queue(0, 0);// 全局队列
    dispatch_async(queue, ^{
        sleep(2);
        dispatch_async(dispatch_get_main_queue(), ^{
            [SVProgressHUD showSuccessWithStatus:@"登陆成功"];
        });
        sleep(1);
        dispatch_async(dispatch_get_main_queue(), ^{
            [SVProgressHUD dismiss];
        });
    });
}
@end
